package com.android.bind.components;

import java.lang.reflect.Field;

import com.android.bind.annotation.BindView;

import android.view.View;

public class ViewHolderBind {

	public static void bind(Object obj, View view) {
		if (obj == null || view == null) {
			return;
		}
		Class<? extends Object> clazz = obj.getClass();
		Field[] fields = clazz.getDeclaredFields();
		if (fields == null || fields.length == 0) {
			return;
		}
		for (Field field : fields) {
			field.setAccessible(true);
			BindView mBindView = field.getAnnotation(BindView.class);
			if (mBindView == null) {
				continue;
			}
			int viewId = mBindView.value();
			if (viewId > 0) {
				try {
					field.set(obj, view.findViewById(viewId));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

}
